<?php
namespace app\test\controller;

use think\Controller;
use think\queue\Job;
use app\common\model\Product\Product;

class Action extends Controller
{
    protected $productModel = null;
    public function __construct()
    {
        $this->productModel = new Product();
        parent::__construct();
    }
    public function fire(Job $job,$data)
    {
        if(!$this->checkstock($data)){
            $job->delete();
            return false;
        }
        $result = $this->orderdone($data);
        if($result){
            $job->delete();
        }else{
            if($job->attempts()>3){
                $job->delete();
                return false;
                // $job->release(2);
            }
        }
    }
    public function checkstock($data)
    {
        $product = $this->productModel->get($data['proid']);
        if($product['stock']-$data['num']<0){
            return false;
        }
        return true;
    }
    public function orderdone($data)
    {
        $product = $this->productModel->get($data['proid']);
        $product['stock'] -= $data['num'];
        $result = $product->isUpdate()->save();
        return boolval($result);
    }
}
